<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <%--<meta charset="UTF-8">--%>
    <title>W to Shopping!</title>
</head>

<body>
<h1>这是一个标签</h1>
<%
    String name=request.getParameter("username");
    String password=request.getParameter("password");
    System.out.println(name);
    System.out.println(password);
    //MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    final String DB_URL = "jdbc:mysql://localhost:3306/gwc?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true";
    // 数据库的用户名与密码，需要根据自己的设置
    final String USER = "root";
    final String PASS = "dd137772";
    Connection conn = null;

    Class.forName(JDBC_DRIVER);
    // 打开链接
    System.out.println("连接数据库...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);
    String sql;

    sql = "SELECT * FROM users where name=? and password=?";
    PreparedStatement stmt=conn.prepareStatement(sql);
    stmt.setString(1,name);
    stmt.setString(2,password);
    ResultSet rs = stmt.executeQuery();

    if (rs.next()){
        request.getRequestDispatcher("main.jsp").forward(request, response);
        String username = rs.getString("name");
        String password1 = rs.getString("password");


        System.out.print("用户名: " + username + "\t");
        System.out.print("密码: " + password1);
        System.out.print("\n");

    }else{
        System.out.println("用户名或密码错误！");
        response.sendRedirect("jsp/failure.jsp");
    }

    System.out.println("Goodbye!");

%>
</body>
</html>